package com.ruoyi.merchant.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.merchant.dto.CustomerHistoryMedicalDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.CustomerHistoryMedical;
import com.ruoyi.merchant.service.ICustomerHistoryMedicalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 客户病史习惯Controller
 * 
 * @author chengxin.ju
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/medical")
public class CustomerHistoryMedicalController extends BaseController
{
    @Autowired
    private ICustomerHistoryMedicalService customerHistoryMedicalService;

    /**
     * 查询客户病史习惯列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerHistoryMedical customerHistoryMedical)
    {
        startPage();
        List<CustomerHistoryMedical> list = customerHistoryMedicalService.selectCustomerHistoryMedicalList(customerHistoryMedical);
        return getDataTable(list);
    }

    /**
     * 导出客户病史习惯列表
     */
    @Log(title = "客户病史习惯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerHistoryMedical customerHistoryMedical)
    {
        List<CustomerHistoryMedical> list = customerHistoryMedicalService.selectCustomerHistoryMedicalList(customerHistoryMedical);
        ExcelUtil<CustomerHistoryMedical> util = new ExcelUtil<CustomerHistoryMedical>(CustomerHistoryMedical.class);
        util.exportExcel(response, list, "客户病史习惯数据");
    }

    /**
     * 获取客户病史习惯详细信息
     */
    @GetMapping(value = "/getById")
    public AjaxResult getInfo(@RequestParam String id)
    {
        CustomerHistoryMedicalDto customerHistoryMedicalDto = customerHistoryMedicalService.selectCustomerHistoryMedicalByID(id);
        return success(customerHistoryMedicalDto);
    }

    /**
     * 新增客户病史习惯
     */
    @Log(title = "客户病史习惯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map map)
    {
        String s = JSONObject.toJSONString(map);
        CustomerHistoryMedicalDto customerHistoryMedicalDto = JSONObject.parseObject(s, CustomerHistoryMedicalDto.class);
        customerHistoryMedicalDto.setId(IdUtils.fastSimpleUUID());
        customerHistoryMedicalDto.setDel(0);
        if (StringUtils.isBlank(customerHistoryMedicalDto.getCustomerId())){
            return error("客户id【customerId】不能为空");
        }
        return toAjax(customerHistoryMedicalService.insertCustomerHistoryMedical(customerHistoryMedicalDto));
    }

    /**
     * 修改客户病史习惯
     */
    @Log(title = "客户病史习惯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Map map)
    {
        String s = JSONObject.toJSONString(map);
        CustomerHistoryMedicalDto customerHistoryMedicalDto = JSONObject.parseObject(s, CustomerHistoryMedicalDto.class);
        if (StringUtils.isBlank(customerHistoryMedicalDto.getId())){
            return error("id不能为空");
        }
        if (StringUtils.isBlank(customerHistoryMedicalDto.getCustomerId())){
            return error("客户id【customerId】不能为空");
        }
        return toAjax(customerHistoryMedicalService.updateCustomerHistoryMedical(customerHistoryMedicalDto));
    }

    /**
     * 删除客户病史习惯
     */
    @Log(title = "客户病史习惯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(customerHistoryMedicalService.deleteCustomerHistoryMedicalByIDs(IDs));
    }
}
